package org.agony2d.gpu.supportClasses.textures {
	import flash.display.BitmapData;
	import flash.display3D.Context3DTextureFormat;
	import org.agony2d.core.agony_internal;
	import org.agony2d.gpu.AAFacade;
	import org.agony2d.gpu.core.AAManager;
	import org.agony2d.notify.AEvent;
	
	use namespace agony_internal;
	
public class RenderableTexture extends TextureProxy {
	
	public function RenderableTexture() {
		AAFacade.addEventListener(AEvent.RESET, ____onContextReset, -22000)
	}
	
	override agony_internal function dispose() : void {
		AAFacade.removeEventListener(AEvent.RESET, ____onContextReset)
		super.dispose()
	}
	
	agony_internal function reset( width:int, height:int ) : void {
		var width_A:int, height_A:int
		var width_B:int, height_B:int
		var BA:BitmapData
		
		width_A       =  width
		height_A      =  height
		width_B       =  getNextPowerOfTwo(width_A)
		height_B      =  getNextPowerOfTwo(height_A)
		m_rawTexture  =  AAManager.g_context3d.createTexture(width_B, height_B, Context3DTextureFormat.BGRA, true)
		if (width_B > width_A || height_B > height_A) {
			BA               =  new BitmapData(width_B, height_B, true, 0x0)
			m_realityWidth   =  width_A
			m_realityHeight  =  height_A
			m_fullWidth      =  width_B
			m_fullHeight     =  height_B
		}
		else {
			m_fullWidth   =  m_realityWidth   =  width_B
			m_fullHeight  =  m_realityHeight  =  height_B
		}
		m_rawTexture.uploadFromBitmapData(BA)
	}
	
	agony_internal function ____onContextReset( e:AEvent ) : void {
		this.reset(m_realityWidth, m_realityHeight)
	}
}
}